package com.fancy.applicaiton.tools.htmltox.wrapper.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;
import java.util.ResourceBundle;

public class ConfigUtil {
    private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class);
    private static final Properties config = new Properties();
    private static Boolean isOuterConfig = Boolean.TRUE;
    static{
        try {
            InputStream configFileIS = new FileInputStream(getConfigPath());
            InputStreamReader configFileReader = new InputStreamReader(configFileIS, "UTF-8");
            loadProperties(config, configFileReader);
        } catch (FileNotFoundException e) {
            logger.warn("未找到外部配置文件..");
            isOuterConfig = Boolean.FALSE;
        } catch (UnsupportedEncodingException e) {
            logger.warn("编码格式不正确..");
            isOuterConfig = Boolean.FALSE;
        } catch (IOException e) {
            isOuterConfig = Boolean.FALSE;
        }
    }
    private static void loadProperties(Properties props, Reader isr) throws IOException {
        props.load(isr);
    }


    private static String  getConfigPath(){
        String path = System.getProperty("user.dir");
        String configPath ="\\config\\config.properties";
        configPath = path + configPath;
        return configPath;
    }

    /**
     * 获取系统文件的配置信息
     * @return
     */
    public static String getAppConfig(String key){
        if(isOuterConfig){
            return config.getProperty(key);
        }else{
            String resource_name ="config";
            ResourceBundle rb = ResourceBundle.getBundle("config."+resource_name);
            String rtnVal = "";
            try {
                rtnVal = rb.getString(key);
            } catch (Exception e) {
                logger.error("获取资源文件错误:"+key);
            }
            return rtnVal;
        }


    }
}
